package com.huawei.appmarket.oobe.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.InflateException;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewParent;
import android.view.ViewStub;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.huawei.appmarket.R;
import com.huawei.appmarket.service.webview.js.HwHiAppPrivacyJs;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.secure.android.common.intent.SafeIntent;
import com.huawei.secure.android.common.webview.SafeWebSettings;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import o.ayy;
import o.azb;
import o.bmm;
import o.dlu;
import o.dlx;
import o.Logger;
import o.ContentManager;
import o.eqp;
import o.ActivityUtil;
import o.evd;

public class OOBEBaseServiceActivity extends Activity {
    private WebViewClient ʼ = new WebViewClient();
    private String ˊ;
    private WebView ˋ;
    private WebView ˎ;
    private WebViewClient ˏ = new WebViewClient(this) {
        final /* synthetic */ OOBEBaseServiceActivity ˏ;

        {
            this.ˏ = r1;
        }

        public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
            super.onPageStarted(webView, str, bitmap);
            Logger.ˏ("OOBEBaseServiceActivity", "onPageStarted:");
        }

        public boolean shouldOverrideUrlLoading(WebView webView, String str) {
            Logger.ˏ("OOBEBaseServiceActivity", "shouldOverrideUrlLoading:");
            return true;
        }

        public void onPageFinished(WebView webView, String str) {
            Logger.ˎ("OOBEBaseServiceActivity", "webview onPageFinished");
            this.ˏ.ॱ.setProgress(0);
            Logger.ˎ("OOBEBaseServiceActivity", "webview onPageFinished showWebView");
            super.onPageFinished(webView, str);
        }

        public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
            super.onReceivedError(webView, webResourceRequest, webResourceError);
            Logger.ˎ("OOBEBaseServiceActivity", "webview onReceivedError");
        }

        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            super.onReceivedSslError(webView, sslErrorHandler, sslError);
            Logger.ˎ("OOBEBaseServiceActivity", "webview onReceivedSslError ");
        }
    };
    private ProgressBar ॱ;

    static class a implements OnTouchListener {
        a() {
        }

        @SuppressLint({"ClickableViewAccessibility"})
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return true;
        }
    }

    static class d {
        private d() {
        }

        @JavascriptInterface
        public boolean needDisplay() {
            return false;
        }
    }

    static class e implements OnLongClickListener {
        e() {
        }

        public boolean onLongClick(View view) {
            return true;
        }
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (!ˎ()) {
            int identifier = Resources.getSystem().getIdentifier("androidhwext:style/Theme.Emui.NoTitleBar", null, null);
            if (identifier != 0) {
                setTheme(identifier);
            }
            ContentManager.build(getApplication());
            ayy.ˋ().ॱ();
            evd.ˋ(this, R.color.emui_white, -1);
            try {
                setContentView(R.layout.activity_oobe_base_service);
                ˎ(-1);
                this.ॱ = (ProgressBar) findViewById(R.id.webview_progressbar);
                this.ॱ.setVisibility(0);
                this.ˋ = (WebView) findViewById(R.id.uri_webview);
                this.ˎ = (WebView) findViewById(R.id.wvTerms);
                ˋ(this.ˋ, this.ˏ, true);
                ˋ(this.ˎ, this.ʼ, false);
                ˏ();
                ˊ();
            } catch (InflateException e) {
                Logger.e("OOBEBaseServiceActivity", "Inflate webview throw Exception");
                finish();
            }
        }
    }

    public void finish() {
        try {
            super.finish();
        } catch (Throwable th) {
            Logger.e("OOBEBaseServiceActivity", "Activity finish error");
        }
    }

    @SuppressLint({"ClickableViewAccessibility"})
    private void ˋ(WebView webView, WebViewClient webViewClient, boolean z) {
        SafeWebSettings.initWebviewAndSettings(webView);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(false);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(false);
        webView.setWebViewClient(webViewClient);
        webView.setLongClickable(false);
        webView.setOnLongClickListener(new e());
        webView.setOnTouchListener(new a());
        if (z) {
            webView.setWebChromeClient(new WebChromeClient(this) {
                final /* synthetic */ OOBEBaseServiceActivity ॱ;

                {
                    this.ॱ = r1;
                }

                public void onProgressChanged(WebView webView, int i) {
                    if (i == 100) {
                        this.ॱ.ॱ.setVisibility(8);
                    } else {
                        if (this.ॱ.ॱ.getVisibility() != 0) {
                            this.ॱ.ॱ.setVisibility(0);
                        }
                        this.ॱ.ॱ.setProgress(i);
                    }
                    super.onProgressChanged(webView, i);
                }
            });
        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new d(), HwHiAppPrivacyJs.JS_CHECK_MORE_NAME);
        webView.addJavascriptInterface(new HwHiAppPrivacyJs(this), HwHiAppPrivacyJs.JS_AGRATTR_NAME);
    }

    private boolean ˎ() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            Logger.e("OOBEBaseServiceActivity", "intent error");
            return true;
        }
        this.ˊ = new SafeIntent(intent).getAction();
        Logger.ˏ("OOBEBaseServiceActivity", "mAction:" + this.ˊ);
        if ("com.huawei.appmarket.oobe.local.user.protocol".equals(this.ˊ) || "com.huawei.appmarket.oobe.local.privacy".equals(this.ˊ) || "com.huawei.appmarket.oobe.local.emui9".equals(this.ˊ)) {
            return false;
        }
        finish();
        Logger.e("OOBEBaseServiceActivity", "unkown action");
        return true;
    }

    protected void onNewIntent(Intent intent) {
        if (intent == null) {
            finish();
            return;
        }
        super.onNewIntent(intent);
        setIntent(intent);
        Logger.ˏ("OOBEBaseServiceActivity", "on new intent");
        if (!ˎ()) {
            ˊ();
        }
    }

    private void ˊ() {
        if (this.ˋ != null) {
            this.ˋ.scrollTo(0, 0);
        }
        if (this.ˎ != null) {
            this.ˎ.scrollTo(0, 0);
        }
        ˋ();
    }

    private void ˋ() {
        new Handler(Looper.getMainLooper()).post(new Runnable(this) {
            final /* synthetic */ OOBEBaseServiceActivity ॱ;

            {
                this.ॱ = r1;
            }

            public void run() {
                try {
                    String canonicalPath = new File(this.ॱ.getCacheDir(), "OobeStatement").getCanonicalPath();
                    File ˎ = dlx.ˎ("oobe/oobe-statement.zip", canonicalPath, this.ॱ, true);
                    if (ˎ == null || !ˎ.exists()) {
                        Logger.e("OOBEBaseServiceActivity", "zip failed");
                        this.ॱ.finish();
                        return;
                    }
                    String ˊ = this.ॱ.ॱ();
                    if (this.ॱ.ॱ(this.ॱ, "CN", ˊ)) {
                        canonicalPath = canonicalPath + "/cn/";
                    } else if ("RU".equalsIgnoreCase(ˊ)) {
                        canonicalPath = canonicalPath + "/ru/";
                    } else if (this.ॱ.ॱ(this.ॱ, "hongkong", ˊ)) {
                        canonicalPath = canonicalPath + "/hk/";
                    } else if (this.ॱ.ॱ(this.ॱ, "europe", ˊ)) {
                        canonicalPath = canonicalPath + "/eur/";
                    } else if (this.ॱ.ॱ(this.ॱ, "north america", ˊ)) {
                        canonicalPath = canonicalPath + "/north-america/";
                    } else if (this.ॱ.ॱ(this.ॱ, "korea", ˊ)) {
                        canonicalPath = canonicalPath + "/korea/";
                    } else {
                        Logger.e("OOBEBaseServiceActivity", "No service location found");
                        this.ॱ.finish();
                        return;
                    }
                    String str = "privacy-statement-" + dlu.ॱ() + ".htm";
                    ˊ = canonicalPath + str;
                    if (!new File(ˊ).exists()) {
                        Logger.ˎ("OOBEBaseServiceActivity", "" + str + " region not found, use default. productCountry:" + eqp.ͺ());
                        ˊ = canonicalPath + "privacy-statement.htm";
                    }
                    String str2 = "terms-" + dlu.ॱ() + ".htm";
                    str = canonicalPath + str2;
                    if (new File(str).exists()) {
                        canonicalPath = str;
                    } else {
                        Logger.ˎ("OOBEBaseServiceActivity", "" + str2 + " region not found, use default. productCountry:" + eqp.ͺ());
                        canonicalPath = canonicalPath + "terms.htm";
                    }
                    this.ॱ.ˋ("file://" + ˊ, "file://" + canonicalPath);
                } catch (IOException e) {
                    Logger.e("OOBEBaseServiceActivity", "getCanonicalPath throw");
                    this.ॱ.finish();
                }
            }
        });
    }

    private void ˋ(final String str, final String str2) {
        runOnUiThread(new Runnable(this) {
            final /* synthetic */ OOBEBaseServiceActivity ॱ;

            public void run() {
                if (!ActivityUtil.isAcitivtyNotValid(this.ॱ)) {
                    if (TextUtils.isEmpty(this.ॱ.ˊ)) {
                        Logger.e("OOBEBaseServiceActivity", "action is empty");
                        this.ॱ.finish();
                    } else if ("com.huawei.appmarket.oobe.local.emui9".equals(this.ॱ.ˊ)) {
                        if (!(this.ॱ.ˋ == null || TextUtils.isEmpty(str2))) {
                            this.ॱ.ˋ.loadUrl(str2);
                        }
                        if (this.ॱ.ˎ != null && !TextUtils.isEmpty(str)) {
                            this.ॱ.ˎ.setVisibility(0);
                            this.ॱ.ˎ.loadUrl(str);
                        }
                    } else if ("com.huawei.appmarket.oobe.local.user.protocol".equals(this.ॱ.ˊ)) {
                        if (this.ॱ.ˋ != null && !TextUtils.isEmpty(str2)) {
                            this.ॱ.ˋ.loadUrl(str2);
                        }
                    } else if ("com.huawei.appmarket.oobe.local.privacy".equals(this.ॱ.ˊ) && this.ॱ.ˋ != null && !TextUtils.isEmpty(str)) {
                        this.ॱ.ˋ.loadUrl(str);
                    }
                }
            }
        });
    }

    @NonNull
    private static List<String> ˏ(Context context, String str) {
        List<String> arrayList = new ArrayList();
        if (context == null || TextUtils.isEmpty(str)) {
            return arrayList;
        }
        if (str.equalsIgnoreCase("CN")) {
            return new ArrayList(Collections.singletonList("CN"));
        }
        try {
            PackageItemInfo applicationInfo = context.getPackageManager().getApplicationInfo("com.huawei.appmarket", DnsConfig.MAX_CACHE_ENTRIES);
            if (applicationInfo.metaData == null) {
                return arrayList;
            }
            String str2 = "";
            if (str.contentEquals("hongkong")) {
                str2 = "statement_hk_service_country";
            } else if (str.contentEquals("europe")) {
                str2 = "statement_eur_service_country";
            } else if (str.contentEquals("north america")) {
                str2 = "statement_north_america_service_country";
            } else if (str.contentEquals("korea")) {
                str2 = "statement_korea_service_country";
            }
            str2 = applicationInfo.metaData.getString(str2);
            if (str2 == null || str2.length() == 0) {
                return arrayList;
            }
            for (String str3 : str2.split(",")) {
                if (str3 != null) {
                    Object trim = str3.trim();
                    if (!TextUtils.isEmpty(trim)) {
                        arrayList.add(trim.toUpperCase(Locale.ENGLISH));
                    }
                }
            }
            return arrayList;
        } catch (NameNotFoundException e) {
            Logger.e("OOBEBaseServiceActivity", "getServiceCountryList exception.");
            return arrayList;
        }
    }

    private boolean ॱ(Context context, String str, String str2) {
        if (TextUtils.isEmpty(str2)) {
            return false;
        }
        return ˏ(context, str).contains(str2.toUpperCase(Locale.ENGLISH));
    }

    private String ॱ() {
        String ˎ = bmm.getSystemProperty("ro.product.locale.language");
        CharSequence ˎ2 = bmm.getSystemProperty("ro.product.locale.region");
        Object obj = ("en".equalsIgnoreCase(ˎ) && "US".equalsIgnoreCase(ˎ2)) ? 1 : null;
        if (TextUtils.isEmpty(ˎ2) || obj != null) {
            return Locale.getDefault().getCountry().toUpperCase(Locale.ENGLISH);
        }
        return ˎ2;
    }

    private void ˎ(int i) {
        ViewStub viewStub = (ViewStub) findViewById(R.id.hwtoolbar_icon1);
        if (viewStub != null) {
            viewStub.inflate();
        }
        TextView textView = (TextView) findViewById(R.id.action_bar_title);
        if (textView != null) {
            if (i != -1) {
                textView.setText(i);
            } else {
                textView.setText(null);
            }
        }
        ViewParent parent = ((LinearLayout) findViewById(R.id.titleContainer)).getParent();
        if (parent instanceof View) {
            azb.ˏ((View) parent);
        }
        ImageView imageView = (ImageView) findViewById(16908295);
        if (imageView != null) {
            imageView.setImageResource(R.drawable.ic_public_back);
            imageView.setVisibility(0);
            imageView.setOnClickListener(new OnClickListener(this) {
                final /* synthetic */ OOBEBaseServiceActivity ˊ;

                {
                    this.ˊ = r1;
                }

                public void onClick(View view) {
                    this.ˊ.finish();
                }
            });
        }
    }

    protected void onResume() {
        super.onResume();
        ˏ(getWindow());
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    private void ˏ(Window window) {
        if (window != null) {
            View decorView = window.getDecorView();
            decorView.setSystemUiVisibility((decorView.getSystemUiVisibility() | 2) | 4096);
        }
    }

    private void ˏ() {
        View findViewById = findViewById(R.id.uri_webview);
        azb.ˋ(findViewById, ˎ(findViewById) + azb.ᐝ(this), ˋ(findViewById) + azb.ʽ(this));
    }

    private int ˎ(View view) {
        if (view == null) {
            return 0;
        }
        LayoutParams layoutParams = view.getLayoutParams();
        return layoutParams instanceof MarginLayoutParams ? ((MarginLayoutParams) layoutParams).leftMargin : 0;
    }

    private int ˋ(View view) {
        if (view == null) {
            return 0;
        }
        LayoutParams layoutParams = view.getLayoutParams();
        return layoutParams instanceof MarginLayoutParams ? ((MarginLayoutParams) layoutParams).rightMargin : 0;
    }
}
